package date;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateDemo {

  public static void main(String[] args) throws ParseException {

    //科普
    /*
    * 格林尼治标准时间（GMT）
    *
      UTC:世界协调时间，也就是零时区的时间
      CST:中国标准时间，中国是东八区，故比UTC大8小时
    * */

    String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC);
    //必须设置 时区
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    // 这样写时区才能生效
    String createdDateTime = "2001-07-04T12:08:56.235+0700";
    Date dateTime = sdf.parse(createdDateTime);

    // CST可以代表上海 Asia/Shanghai
    // Wed Jul 04 13:08:56 CST 2001
    System.out.println(dateTime);
  }

  @Test
  public void test() throws ParseException {

    String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC);
    //不设置时区 默认使用 TimeZone.getDefault()

    String createdDateTime = "2021-02-17T12:59:58.553";
    Date dateTime = sdf.parse(createdDateTime);
//    Wed Feb 17 12:59:58 CST 2021
    System.out.println(dateTime);
  }

  @Test
  public void test2() throws ParseException {
    String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC);
    //设置时区  这个地方小心 CST 代表 America/Chicago utc -6
    sdf.setTimeZone(TimeZone.getTimeZone("CST"));

    String createdDateTime = "2021-02-17T03:01:58.553";
    Date dateTime = sdf.parse(createdDateTime);
    // Wed Feb 17 20:59:58 CST 2021 这里的CST代表 Asia/Shanghai utc+8
    System.out.println(dateTime);
  }

  @Test
  public void test3() throws ParseException {

    String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC);

    String createdDateTime = "2021-02-17T12:59:58.553";
    Date dateTime = sdf.parse(createdDateTime);
//  Wed Feb 17 20:59:58 CST 2021
    System.out.println(dateTime);
  }

  @Test
  public void testzone() {
    TimeZone cst = TimeZone.getTimeZone("CST");
    // 那到的时区 是 America/Chicago
    System.out.println(cst);
    //查询失败，默认使用GMT
    TimeZone cst1 = TimeZone.getTimeZone("+08:00");
    //id="GMT"
    System.out.println(cst1);

    TimeZone aDefault = TimeZone.getDefault();
    System.out.println(aDefault);
  }




}
